/// <reference path="references.js" />

if (window.irinori === undefined)
{
    window.irinori = {};
}

//Should be refactored to AnimationData
irinori.MovementData = function(a_robot, a_movementDataType)
{   
    ///<summary>Create this class by using the Create functions</summary> 
    this.m_robot = a_robot;

    //this.m_oldX; 
    //this.m_oldY;

    //this.m_newX; 
    //this.m_newY;

    //this.m_oldDirection;
    //this.m_newDirection;
    
    //this.m_causeOfDeath;

    this.m_type = a_movementDataType;

}

irinori.MovementData.Type = { "Movement" : 0, "Rotation" : 1, "Death" : 2  };
irinori.MovementData.CauseOfDeath = { "Hole" : 0 , "Laser" : 1, "Other" : 2};

irinori.MovementData.CreateMovement = function(a_robot, a_oldX, a_oldY, a_newX, a_newY)
{
    if (!a_robot)
    {
        throw "irinori.MovementData a_robot must be defined";
    }
    else if (a_oldX === undefined)
    {
        throw "irinori.MovementData a_oldX must be a number";
    }
    else if (a_oldY === undefined)
    {
        throw "irinori.MovementData a_oldY must be a number";
    }
    else if (a_newX === undefined)
    {
        throw "irinori.MovementData a_newX must be a number";
    }
    else if (a_newY === undefined)
    {
        throw "irinori.MovementData a_newY must be a number";
    }

    var f_data = new irinori.MovementData(a_robot, irinori.MovementData.Type.Movement);
    f_data.m_oldX = a_oldX;
    f_data.m_oldY = a_oldY;

    f_data.m_newX = a_newX;
    f_data.m_newY = a_newY;    

    return f_data;
}

irinori.MovementData.CreateRotation = function(a_robot, a_oldDirection, a_newDirection)
{
    var f_data = new irinori.MovementData(a_robot, irinori.MovementData.Type.Rotation);
    f_data.m_oldDirection = a_oldDirection;
    f_data.m_newDirection = a_newDirection;

    //As it's rotation
    //f_data.m_movement = false;

    return f_data;
}

irinori.MovementData.CreateDeath = function(a_robot, a_causeOfDeath)
{
    if (!a_robot)
    {
        throw "irinori.MovementData.CreateDeath : a_robot is undefined";
    }

    var f_data = new irinori.MovementData(a_robot, irinori.MovementData.Type.Death);
    f_data.m_causeOfDeath = a_causeOfDeath;
    f_data.m_lives = a_robot.m_lives - 1;

    return f_data;
}

irinori.RobotPosition = function(a_robot, a_name)
{
    this.m_robot = a_robot;
    this.m_posX = a_robot.m_posX;
    this.m_posY = a_robot.m_posY;

    this.m_direction = a_robot.m_direction;

    this.m_alive = a_robot.m_alive;
    this.m_lives = a_robot.m_lives;

    this.m_spawnX = a_robot.m_spawnX;
    this.m_spawnY = a_robot.m_spawnY;

    this.m_playername = a_name;
}

//********************
//MinorCycleCollection
//*********************
irinori.MinorCycleCollection = function()
{
    this.m_currentCollection = [];
    this.m_collection = [];
    this.m_robotPositions = [];
}

irinori.MinorCycleCollection.prototype.AddData = function(a_movementData)
{
    this.m_currentCollection.push(a_movementData);
}

irinori.MinorCycleCollection.prototype.AddRobotPosition = function (a_robot, a_player)
{
    this.m_robotPositions.push(new irinori.RobotPosition(a_robot, a_player.m_name) );
}


irinori.MinorCycleCollection.prototype.NewSegment = function()
{
    ///<summary>Adds a new segment to the minorcycle collection, like after robots moved on conveyor belts e.t.c.! </summary>
    for (var i = 0; i < this.m_currentCollection.length; i++) 
    {
        this.m_collection.push(this.m_currentCollection[i] );
    }

    //this.m_collection.push(this.m_currentCollection);

    this.m_currentCollection = [];
}

//********************
//MajorCycleCollection
//*********************

//Has an array of MovementDataCollection

irinori.MajorCycleCollection = function()
{
    this.m_currentMinorCollection = new irinori.MinorCycleCollection();

    //An array with MovementCollection based on registers
    this.m_minorCollection = [];
}

//Adds a movementdata to the currentMovementcollection! 
irinori.MajorCycleCollection.prototype.AddAnimationData = function(a_movementData)
{
    this.m_currentMinorCollection.AddData(a_movementData);
}

irinori.MajorCycleCollection.prototype.AddRobotPosition = function(a_robot, a_player)
{
    this.m_currentMinorCollection.AddRobotPosition(a_robot, a_player);
}


//Should be called at end of DoMinorCycle
irinori.MajorCycleCollection.prototype.AddMinorCycle = function()
{
    
    if (this.m_currentMinorCollection.m_currentCollection.length > 0)
    {
        this.m_currentMinorCollection.NewSegment();
    }

    if (this.m_currentMinorCollection.m_collection.length > 0)
    {        
        this.m_minorCollection.push(this.m_currentMinorCollection);

        this.m_currentMinorCollection = new irinori.MinorCycleCollection();  
    }    
}

irinori.MajorCycleCollection.prototype.ClearCollection = function()
{
    this.m_minorCollection = [];

    this.m_currentMinorCollection = new irinori.MinorCycleCollection();
}